package com.mzba.place.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mzba.place.R;

public class ColorLump extends LinearLayout {
	
	private final static int DEFAULT_TEXT_COLOR = 0xffa2a7ad;
	private TextView tvColorLumpTitle1 = null;
	private TextView tvColorLumpTitle2 = null;
	private LinearLayout colorLayout;
	private ColorLumpData data = null;

	private Boolean isLayoutInited = false;
	private Boolean isDataLoadNeeded = false;

	public ColorLump(Context context) {
		super(context);
		init();
	}

	public ColorLump(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ColorLump(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.addView(LayoutInflater.from(getContext()).inflate(R.layout.color_lump_layout, null));
		tvColorLumpTitle1 = (TextView) this.findViewById(R.id.card_count);
		tvColorLumpTitle2 = (TextView) this.findViewById(R.id.card_description);
		colorLayout = (LinearLayout) this.findViewById(R.id.colorlump_layout);
	}

	public int getColor() {
		return data.color;
	}

	public void setColor(int color) {
		this.data.color = color;
	}

	public void setData(ColorLumpData colorLumpData) {
		if (null == colorLumpData)
			return;
		this.data = colorLumpData;
		isDataLoadNeeded = true;
		if (isLayoutInited) {
			LoadData();
		}
	}

	private void LoadData() {
		isDataLoadNeeded = false;
		colorLayout.setBackgroundColor(data.color);
		if (data.title.equals("0")) {
			tvColorLumpTitle1.setText("+");
			tvColorLumpTitle2.setText("安全卡片");
		} else {
			tvColorLumpTitle1.setText(data.title);
			tvColorLumpTitle2.setText(data.content);
		}
	}

	public String getSpace(int count) {
		String str = "";
		for (int i = 0; i < count; i++) {
			str += " ";
		}
		return str;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		if (changed) {
			if (this.getWidth() * this.getHeight() == 0) {
				isLayoutInited = false;
			} else {
				isLayoutInited = true;
			}
		}
		if (isLayoutInited && isDataLoadNeeded) {
			this.LoadData();
		}
	}

	public static class ColorLumpData {
		public static int NONE = -1;
		public String title = null;
		public String content = null;
		public int color = DEFAULT_TEXT_COLOR;

		public ColorLumpData(String title, int color, String content) {
			this.title = title;
			if (color != NONE) {
				this.color = color;
			}
			this.content = content;
		}
	}
}
